/**
 * @author: 王志昂
 * @create: 2014年6月5日
 * @note: 前端JS公共模块
 * @history:
 * 2014年6月5日 11:39 创建
 */
define(function (require, exports, module) {
    require('jquery');
    require('underscore');
    require("pop")($);
    require("select2")($);
    var ItUtil = {
        peopleOptionTpl: '<% for (var i in peopleList) { var people = peopleList[i]; if ((typeof occupation != "undefined" && !_.contains(occupation, people.role)) || people.realname == "admin") {continue;} %><option value="<%= people.id %>" <%= _.contains(people_id, people.id) ? "selected" : "" %>><%= (people.nickname ? people.nickname : people.realname) + "--" + people.name %></option><% }; %>',
        ecsOptionTpl: '<% for (var i in ecsList) { var ecs = ecsList[i]["info"]; %><option value="<%= ecs.id %>" <%= _.contains(ecs_id, ecs.id) ? "selected" : "" %>><%= ecs.hostname %></option><% }; %>',
        mysqlMasterOptionTpl: '<option value="0">请选择</option><% for (var i in mysqlList) { var mysql = mysqlList[i]; %><option value="<%= mysql.id %>" <%= _.contains(masterId, mysql.id) ? "selected" : "" %>><%= mysql.hostname + "-" + mysql.inst_role %></option><% }; %>',
        // Ecs开始
        renderAddEcsWindowPeopleSelect: function (peopleList, users) {
            users = users || "";
            var usersArr = users.split(",");
            var usersOption = _.template(this.peopleOptionTpl, {
                peopleList: peopleList,
                people_id: usersArr,
                _: _
            });
            $('#add_ecs_window_users').html(usersOption).select2();
            $('#add_ecs_window_environment').select2();
        },
        renderAddWindowEcsSelect: function (ecsList, ecs_id, type) {
            ecs_id = ecs_id || 0;
            var ecsOption = _.template(this.ecsOptionTpl, {
                ecsList: ecsList,
                ecs_id: [ecs_id],
                _: _
            });
            $('#add_' + type + '_window_ecs_id').html(ecsOption).select2();
        },
        getEcsList: function (type) {
            var self = this;
            $.ajax({
                url: domain + '/home/it/get_ecs',
                method: 'GET',
                dataType: 'json',
                success: function (data) {
                    var ecsList = data.data;
                    self.renderAddWindowEcsSelect(ecsList, 0, type);
                }
            });
        },
        getMysqlMasterList: function () {
            var self = this;
            $.ajax({
                url: domain + '/home/it/get_mysql_master_list',
                method: 'GET',
                dataType: 'json',
                success: function (data) {
                    var masterList = data.data;
                    self.renderAddWindowMysqlMasterSelect(masterList);
                }
            });
        },

        refreshList: function () {
            window.location.reload();
        },
        bindAddEcsButtonEvent: function () {
            $('#addEcsBtn').click(function () {
                $('#add_ecs_window').popup({
                    filter: '#mask',
                    zindex: 9990
                });
            });
        },
        bindAddEcsWindowCloseEvent: function () {
            var self = this;
            $('#add_ecs_window').find('.remove').click(function () {
                $('#add_ecs_window').pophide({
                    filter: '#mask'
                });
            });
            $('#add_ecs_window_cancel_btn').click(function () {
                $('#add_ecs_window').pophide({
                    filter: '#mask'
                });
            });
        },
        bindSaveEcsButtonEvent: function (callback) {
            var self = this;
            $('#add_ecs_window_save_btn').click(function () {
                var id = $('#add_ecs_window_id').val();
                var hostname = $('#add_ecs_window_hostname').val();
                var inner_ip = $('#add_ecs_window_inner_ip').val();
                var outer_ip = $('#add_ecs_window_outer_ip').val();
                var config = $('#add_ecs_window_config').val();
                var location = $('#add_ecs_window_location').val();
                var environment = $('#add_ecs_window_environment').val();
                var os = $('#add_ecs_window_os').val();
                var port = $('#add_ecs_window_port').val();
                var used_for = $('#add_ecs_window_used_for').val();
                var users = $('#add_ecs_window_users').val();

                $.ajax({
                    url: domain + '/home/it/save_ecs',
                    method: 'GET',
                    dataType: 'json',
                    data: {
                        id: id,
                        hostname: hostname,
                        inner_ip: inner_ip,
                        outer_ip: outer_ip,
                        config: config,
                        location: location,
                        environment: environment,
                        os: os,
                        port: port,
                        used_for: used_for,
                        users: users
                    },
                    success: function (data) {
                        alert(data.errorMsg);
                        if (data.errorCode == 0) {
                            $('#add_ecs_window').find('.remove').click();
                            callback();
                        }
                    }
                });
            });
        },
        // Ecs结束

        // Mysql开始
        renderAddWindowMysqlMasterSelect: function (mysqlList, masterId) {
            masterId = masterId || 0;
            var masterOption = _.template(this.mysqlMasterOptionTpl, {
                mysqlList: mysqlList,
                masterId: [masterId],
                _: _
            });
            $('#add_mysql_window_master_id').html(masterOption).select2();
        },
        bindAddMysqlButtonEvent: function () {
            $('#addMysqlBtn').click(function () {
                $('#add_mysql_window').popup({
                    filter: '#mask',
                    zindex: 9990
                });
            });
        },
        bindAddMysqlWindowCloseEvent: function () {
            var self = this;
            $('#add_mysql_window').find('.remove').click(function () {
                $('#add_mysql_window').pophide({
                    filter: '#mask'
                });
            });
            $('#add_mysql_window_cancel_btn').click(function () {
                $('#add_mysql_window').pophide({
                    filter: '#mask'
                });
            });
        },
        bindAddMysqlSelectSlaveEvent: function () {
            $('#add_mysql_window_role').change(function () {
                if ($(this).val() == 'Slave') {
                    $('#add_mysql_window_master_id').prop('disabled', false);
                } else {
                    $("#add_mysql_window_master_id option[value='0']").siblings().attr('selected', false).attr("selected", true);
                    $('#add_mysql_window_master_id').select2().prop('disabled', true);
                }
            });
        },
        bindSaveMysqlButtonEvent: function (callback) {
            var self = this;
            $('#add_mysql_window_save_btn').click(function () {
                var id = $('#add_mysql_window_id').val();
                var ecs_id = $('#add_mysql_window_ecs_id').val();
                var port = $('#add_mysql_window_port').val();
                var env_type = $('#add_mysql_window_environment').val();
                var inst_role = $('#add_mysql_window_role').val();
                var master_id = $('#add_mysql_window_master_id').val();
                var extend_desc = $('#add_mysql_window_used_for').val();
                var version = $('#add_mysql_window_version').val();
                var datadir = $('#add_mysql_window_datadir').val();
                var basedir = $('#add_mysql_window_basedir').val();
                var conf_path = $('#add_mysql_window_conf_path').val();
                var command = $('#add_mysql_window_command').val();
                // @todo schema_path user_path

                $.ajax({
                    url: domain + '/home/it/save_mysql',
                    method: 'GET',
                    dataType: 'json',
                    data: {
                        id: id,
                        ecs_id: ecs_id,
                        port: port,
                        env_type: env_type,
                        inst_role: inst_role,
                        master_id: master_id,
                        extend_desc: extend_desc,
                        version: version,
                        datadir: datadir,
                        basedir: basedir,
                        conf_path: conf_path,
                        command: command
                    },
                    success: function (data) {
                        alert(data.errorMsg);
                        if (data.errorCode == 0) {
                            $('#add_mysql_window').find('.remove').click();
                            callback();
                        }
                    }
                });
            });
        },
        // Mysql结束

        // Nginx开始
        renderAddWindowNginxMasterSelect: function (nginxList, masterId) {
            masterId = masterId || 0;
            var masterOption = _.template(this.nginxMasterOptionTpl, {
                nginxList: nginxList,
                masterId: [masterId],
                _: _
            });
            $('#add_nginx_window_master_id').html(masterOption).select2();
        },
        bindAddNginxButtonEvent: function () {
            $('#addNginxBtn').click(function () {
                $('#add_nginx_window').popup({
                    filter: '#mask',
                    zindex: 9990
                });
            });
        },
        bindAddNginxWindowCloseEvent: function () {
            var self = this;
            $('#add_nginx_window').find('.remove').click(function () {
                $('#add_nginx_window').pophide({
                    filter: '#mask'
                });
            });
            $('#add_nginx_window_cancel_btn').click(function () {
                $('#add_nginx_window').pophide({
                    filter: '#mask'
                });
            });
        },
        bindAddNginxSelectSlaveEvent: function () {
            $('#add_nginx_window_role').change(function () {
                if ($(this).val() == 'Slave') {
                    $('#add_nginx_window_master_id').prop('disabled', false);
                } else {
                    $("#add_nginx_window_master_id option[value='0']").siblings().attr('selected', false).attr("selected", true);
                    $('#add_nginx_window_master_id').select2().prop('disabled', true);
                }
            });
        },
        bindSaveNginxButtonEvent: function (callback) {
            var self = this;
            $('#add_nginx_window_save_btn').click(function () {
                var id = $('#add_nginx_window_id').val();
                var ecs_id = $('#add_nginx_window_ecs_id').val();
                var port = $('#add_nginx_window_port').val();
                var env_type = $('#add_nginx_window_environment').val();
                var version = $('#add_nginx_window_version').val();
                var config = $('#add_nginx_window_config').val();
                var log = $('#add_nginx_window_log').val();
                var command = $('#add_nginx_window_command').val();

                $.ajax({
                    url: domain + '/home/it/save_nginx',
                    method: 'GET',
                    dataType: 'json',
                    data: {
                        id: id,
                        ecs_id: ecs_id,
                        port: port,
                        env_type: env_type,
                        version: version,
                        config: config,
                        log: log,
                        command: command
                    },
                    success: function (data) {
                        alert(data.errorMsg);
                        if (data.errorCode == 0) {
                            $('#add_nginx_window').find('.remove').click();
                            callback();
                        }
                    }
                });
            });
        },
        // Nginx结束

        // Tomcat开始
        renderAddWindowTomcatMasterSelect: function (tomcatList, masterId) {
            masterId = masterId || 0;
            var masterOption = _.template(this.tomcatMasterOptionTpl, {
                tomcatList: tomcatList,
                masterId: [masterId],
                _: _
            });
            $('#add_tomcat_window_master_id').html(masterOption).select2();
        },
        bindAddTomcatButtonEvent: function () {
            $('#addTomcatBtn').click(function () {
                $('#add_tomcat_window').popup({
                    filter: '#mask',
                    zindex: 9990
                });
            });
        },
        bindAddTomcatWindowCloseEvent: function () {
            var self = this;
            $('#add_tomcat_window').find('.remove').click(function () {
                $('#add_tomcat_window').pophide({
                    filter: '#mask'
                });
            });
            $('#add_tomcat_window_cancel_btn').click(function () {
                $('#add_tomcat_window').pophide({
                    filter: '#mask'
                });
            });
        },
        bindAddTomcatSelectSlaveEvent: function () {
            $('#add_tomcat_window_role').change(function () {
                if ($(this).val() == 'Slave') {
                    $('#add_tomcat_window_master_id').prop('disabled', false);
                } else {
                    $("#add_tomcat_window_master_id option[value='0']").siblings().attr('selected', false).attr("selected", true);
                    $('#add_tomcat_window_master_id').select2().prop('disabled', true);
                }
            });
        },
        bindSaveTomcatButtonEvent: function (callback) {
            var self = this;
            $('#add_tomcat_window_save_btn').click(function () {
                var id = $('#add_tomcat_window_id').val();
                var ecs_id = $('#add_tomcat_window_ecs_id').val();
                var app_name = $('#add_tomcat_window_app_name').val();
                var server_port = $('#add_tomcat_window_server_port').val();
                var shutdown_port = $('#add_tomcat_window_shutdown_port').val();
                var ajp_port = $('#add_tomcat_window_ajp_port').val();
                var debug_port = $('#add_tomcat_window_debug_port').val();
                var ssl_port = $('#add_tomcat_window_ssl_port').val();
                var environment = $('#add_tomcat_window_environment').val();
                var container_name = $('#add_tomcat_window_container_name').val();
                var install_dir = $('#add_tomcat_window_install_dir').val();
                var url = $('#add_tomcat_window_url').val();
                var command = $('#add_tomcat_window_command').val();
                var time = $('#add_tomcat_window_time').val();
                var status = $('#add_tomcat_window_status').val();

                $.ajax({
                    url: domain + '/home/it/save_tomcat',
                    method: 'GET',
                    dataType: 'json',
                    data: {
                        id: id,
                        ecs_id: ecs_id,
                        app_name: app_name,
                        server_port: server_port,
                        shutdown_port: shutdown_port,
                        ajp_port: ajp_port,
                        debug_port: debug_port,
                        ssl_port: ssl_port,
                        environment: environment,
                        container_name: container_name,
                        install_dir: install_dir,
                        url: url,
                        command: command,
                        time: time,
                        status: status
                    },
                    success: function (data) {
                        alert(data.errorMsg);
                        if (data.errorCode == 0) {
                            $('#add_tomcat_window').find('.remove').click();
                            callback();
                        }
                    }
                });
            });
        }
        // Tomcat结束

    };
    module.exports = ItUtil;
});
